package com.honglv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.honglv.context.BaseContext;
import com.honglv.mapper.AttractionsMapper;
import com.honglv.mapper.StrategyMapper;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.UploadAttractionsDTO;
import com.honglv.pojo.entity.Attractions;
import com.honglv.pojo.entity.Attractions;
import com.honglv.pojo.entity.Attractions;
import com.honglv.pojo.vo.GetAttractionsByCityVO;
import com.honglv.pojo.vo.GetAttractionsByCityVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.AttractionsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class AttractionsServiceImpl implements AttractionsService {
    @Autowired
    private AttractionsMapper attractionsMapper;
    /**
     * 管理员上传景点
     * @param
     * @return
     */
    @Override
    public void upload(UploadAttractionsDTO uploadAttractionsDTO) {
        Long authorId = BaseContext.getCurrentId();
        Attractions attractions=new Attractions();
        BeanUtils.copyProperties(uploadAttractionsDTO,attractions);
        attractions.setAuthorId(authorId);
        attractions.setLike(0L);
        attractions.setViews(0L);
        attractionsMapper.insert(attractions);


    }
    /**
     * 根据城市分页获取景点
     * @param
     * @return
     */

    @Override
    public PageVO<GetAttractionsByCityVO> getAttractionsByCity(PageByCityDTO pageByCityDTO) {
        PageHelper.startPage(pageByCityDTO.getPage(),pageByCityDTO.getPageSize());
        QueryWrapper<Attractions> qw=new QueryWrapper<>();
        qw.like("city",pageByCityDTO.getCity());
        List<Attractions> list = attractionsMapper.selectList(qw);
        GetAttractionsByCityVO gf=new GetAttractionsByCityVO();
        List<GetAttractionsByCityVO> attractions=new ArrayList<>();
        for(Attractions Attractions:list){
            BeanUtils.copyProperties(Attractions,gf);
            attractions.add(gf);
        }
        if (attractions == null){
            return null;
        }
        PageInfo<GetAttractionsByCityVO> pageInfo = new PageInfo<>(attractions);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }

}
